<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;


/**
 * App\Models\PostItem
 *
 * @property int $aid 文章ID
 * @property int $uid 会员ID
 * @property string $username 用户名
 * @property int $catid 分类ID
 * @property string $author 作者
 * @property string $type 文章形式
 * @property string $title 文章标题
 * @property string $alias 别名
 * @property string $summary 摘要
 * @property string $image 图片
 * @property string $tags 标签
 * @property string $created_at
 * @property string $updated_at
 * @property int $allowcomment 允许评论
 * @property int $collections 被收藏数
 * @property \Illuminate\Database\Eloquent\Collection|\App\Models\PostComment[] $comments 评论数
 * @property int $views 浏览数
 * @property int $likes 点赞数
 * @property int $status
 * @property string $from 来源
 * @property string $fromurl 来源地址
 * @property int $contents 内容数
 * @property float $price 阅读价格
 * @property int $click1
 * @property int $click2
 * @property int $click3
 * @property int $click4
 * @property int $click5
 * @property int $click6
 * @property int $click7
 * @property-read \App\Models\PostCatlog $catlog
 * @property-read \App\Models\PostContent $content
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\PostImage[] $images
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\PostLog[] $logs
 * @property-read \App\Models\PostMedia $media
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem available()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem unavailable()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereAid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereAlias($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereAllowcomment($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereAuthor($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereCatid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereClick1($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereClick2($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereClick3($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereClick4($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereClick5($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereClick6($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereClick7($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereCollections($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereComments($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereContents($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereFrom($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereFromurl($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereImage($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereLikes($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereSummary($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereTags($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereUsername($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\PostItem whereViews($value)
 * @mixin \Eloquent
 */
class PostItem extends Model
{
    protected $table = 'post_item';
    protected $primaryKey = 'aid';
    protected $guarded    = ['aid'];

    public $timestamps = false;

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub

        static::creating(function (PostItem $postItem){
            if (!$postItem->created_at) $postItem->created_at = time();
            if (!$postItem->updated_at) $postItem->updated_at = time();
        });

        static::updating(function (PostItem $postItem){
            $postItem->updated_at = time();
        });

        static::deleted(function (PostItem $postItem){
            $postItem->content()->delete();
            $postItem->comments()->delete();
            $postItem->media()->delete();
            $postItem->images()->delete();
            $postItem->logs()->delete();
        });
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeAvailable($query)
    {
        return $query->where('status', 1);
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeUnavailable($query)
    {
        return $query->where('status', 0);
    }

    public function content()
    {
        return $this->hasOne(PostContent::class, 'aid', 'aid');
    }

    public function media()
    {
        return $this->hasOne(PostMedia::class, 'aid', 'aid');
    }

    public function images()
    {
        return $this->hasMany(PostImage::class, 'aid', 'aid');
    }

    public function comments()
    {
        return $this->hasMany(PostComment::class, 'aid', 'aid');
    }

    public function catlog()
    {
        return $this->belongsTo(PostCatlog::class, 'catid', 'catid');
    }

    public function logs()
    {
        return $this->hasMany(PostLog::class, 'aid', 'aid');
    }
}
